/**
 *
 * @description 登录、获取用户信息、退出登录、清除accessToken逻辑，不建议修改
 */

import Vue from "vue";
import { getUserInfo, login, logout, getButtonJurisdiction } from "@/api/user";
import {
	getAccessToken,
	removeAccessToken,
	setAccessToken,
} from "@/utils/accessToken";
import { resetRouter } from "@/router";
import { title, tokenName } from "@/config/settings";

const state = {
	accessToken: getAccessToken(),
	uname: "",
	avatar: "",
	permissions: [],
	buttonJurisdiction: []
};
const getters = {
	accessToken: (state) => state.accessToken,
	uname: (state) => state.uname,
	username: (state) => state.uname,
	avatar: (state) => state.avatar,
	permissions: (state) => state.permissions,
	buttonJurisdiction: (state) => state.buttonJurisdiction,

};
const mutations = {
	setAccessToken(state, accessToken) {
		state.accessToken = accessToken;
		setAccessToken(accessToken);
	},
	setusername(state, uname) {
		state.uname = uname;
	},
	setAvatar(state, avatar) {
		state.avatar = avatar;
	},
	setPermissions(state, permissions) {
		state.permissions = permissions;
	},
	setButtonJurisdiction(state, buttonJurisdiction) {
		state.buttonJurisdiction = buttonJurisdiction;
	},
};
const actions = {
	setPermissions({ commit }, permissions) {
		commit("setPermissions", permissions);
	},
	setButtonJurisdiction({ commit }, buttonJurisdiction) {
		commit("setButtonJurisdiction", buttonJurisdiction);
	},
	async login({ commit }, userInfo) {
		const { data } = await login(userInfo);
		const accessToken = data[tokenName];
		if (accessToken) {
			commit("setAccessToken", accessToken);
			const hour = new Date().getHours();
			const thisTime =
				hour < 8
					? "早上好"
					: hour <= 11
					? "上午好"
					: hour <= 13
					? "中午好"
					: hour < 18
					? "下午好"
					: "晚上好";
			Vue.prototype.$baseNotify(`欢迎登录${title}`, `${thisTime}！`);
		} else {
			Vue.prototype.$baseMessage(
				`登录接口异常，未正确返回${tokenName}...`,
				"error"
			);
		}
	},
	async getUserInfo({ commit, state }) {
		let permissions = ['aaa']
		let username = localStorage.username || " "
		let avatar = 'waaaaa'
		if (permissions && Array.isArray(permissions)) {
			commit("setPermissions", permissions);
			commit("setusername", username);
			commit("setAvatar", avatar);


			console.log()
			console.log(permissions)
			return permissions;
		} else {
			Vue.prototype.$baseMessage("用户信息接口异常", "error");
			return false;
		}


		// const { data } = await getUserInfo(state.accessToken);
		// if (!data) {
		// 	Vue.prototype.$baseMessage("验证失败，请重新登录...", "error");
		// 	return false;
		// }



		// let { permissions, username, avatar } = data;
		// if (permissions && username && Array.isArray(permissions)) {
		// 	commit("setPermissions", permissions);
		// 	commit("setusername", username);
		// 	commit("setAvatar", avatar);
		// 	return permissions;
		// } else {
		// 	Vue.prototype.$baseMessage("用户信息接口异常", "error");
		// 	return false;
		// }
	},
	async getButtonJurisdiction({ commit, state }, type) {
		let arr = []
		if(type){
			if(localStorage.buttonJurisdiction){
				arr = JSON.parse(localStorage.buttonJurisdiction)
			}else{
				arr = []
			}
		}else{
			let { data } = await getButtonJurisdiction();
			arr = data
			if(arr && arr.length){
				localStorage.buttonJurisdiction = JSON.stringify(arr)
			}
		}
		commit("setButtonJurisdiction", arr)
	},
	async logout({ dispatch }) {
		// await logout(state.accessToken);
		await dispatch("resetAccessToken");
		await resetRouter();
	},
	resetAccessToken({ commit }) {
		commit("setPermissions", []);
		commit("setAccessToken", "");
		removeAccessToken();
	},
};
export default { state, getters, mutations, actions };
